use eframe::egui::{self, Ui};
use crate::app::{App, state::AppEvent};
use crate::ui::theme::AppTheme;
use crate::ui::widgets::card_frame;

pub struct SettingsPanel;

impl SettingsPanel {
    pub fn render(ui: &mut Ui, app: &App, theme: &AppTheme) -> Option<AppEvent> {
        let mut event = None;
        
        // 标题
        ui.vertical_centered(|ui| {
            ui.add_space(16.0);
            ui.heading("应用设置");
            ui.add_space(16.0);
        });
        
        // 设置内容
        ui.vertical(|ui| {
            // 基本设置
            card_frame(theme, false).show(ui, |ui| {
                ui.heading("基本设置");
                ui.add_space(8.0);
                
                // 开机自启动选项
                ui.horizontal(|ui| {
                    ui.label("开机自启动:");
                    let auto_start = app.app_config.auto_start;
                    ui.label(if auto_start { "开启" } else { "关闭" });
                    if ui.button("切换").clicked() {
                        event = Some(AppEvent::SetAutoStart(!auto_start));
                    }
                });
                
                ui.add_space(8.0);
                
                // 自动连接选项
                ui.horizontal(|ui| {
                    ui.label("自动连接:");
                    let auto_connect = app.app_config.auto_connect;
                    ui.label(if auto_connect { "开启" } else { "关闭" });
                    if ui.button("切换").clicked() {
                        event = Some(AppEvent::SetAutoConnect(!auto_connect));
                    }
                });
                
                ui.add_space(8.0);
                
                // 端口设置
                ui.horizontal(|ui| {
                    ui.label("Socks5端口:");
                    ui.label(format!("{}", app.app_config.socks5_port));
                    if ui.button("修改").clicked() {
                        event = Some(AppEvent::EditSocks5Port);
                    }
                });
                
                ui.add_space(8.0);
                
                // HTTP端口设置
                ui.horizontal(|ui| {
                    ui.label("HTTP端口:");
                    ui.label(format!("{}", app.app_config.http_port));
                    if ui.button("修改").clicked() {
                        event = Some(AppEvent::EditHttpPort);
                    }
                });
            });
            
            ui.add_space(16.0);
            
            // 外观设置
            card_frame(theme, false).show(ui, |ui| {
                ui.heading("外观设置");
                ui.add_space(8.0);
                
                // 暗色模式选项
                ui.horizontal(|ui| {
                    ui.label("暗色模式:");
                    let dark_mode = app.app_config.dark_mode;
                    ui.label(if dark_mode { "开启" } else { "关闭" });
                    if ui.button("切换").clicked() {
                        event = Some(AppEvent::SetDarkMode(!dark_mode));
                    }
                });
                
                ui.add_space(16.0);
                
                ui.label("主题颜色:");
                ui.horizontal(|ui| {
                    // 预设颜色选择
                    let colors = [
                        ((59, 130, 246), "蓝色"),   // 蓝色
                        ((16, 185, 129), "绿色"),   // 绿色
                        ((236, 72, 153), "粉色"),   // 粉色
                        ((245, 158, 11), "橙色"),   // 橙色
                        ((139, 92, 246), "紫色"),    // 紫色
                    ];
                    
                    for (color, name) in colors.iter() {
                        let color32 = egui::Color32::from_rgb(color.0, color.1, color.2);
                        let btn = ui.add(
                            egui::Button::new(
                                egui::RichText::new(*name)
                                    .color(egui::Color32::WHITE)
                                    .strong()
                            )
                            .fill(color32)
                            .min_size(egui::vec2(60.0, 32.0))
                        );
                        
                        if btn.clicked() {
                            event = Some(AppEvent::SetPrimaryColor(*color));
                        }
                    }
                });
            });
            
            ui.add_space(16.0);
            
            // 重置按钮
            ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                if ui.button("重置所有设置").clicked() {
                    event = Some(AppEvent::ResetConfig);
                }
            });
        });
        
        event
    }
} 